﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CacheDemo1.Utility;
namespace CacheDemo1.Controllers
{
    public class SecondController : Controller
    {
        private readonly ILogger<SecondController> _logger;
        private IConfiguration _iConfiguration;
        //普通缓存
        private IMemoryCache _iMemoryCache;
        //分布式缓存
        private IDistributedCache _iDistributedCache;
        //自定义缓存
        private ICustomMemoryCache _iCustomMemoryCache;

        public SecondController(ILogger<SecondController> logger,IConfiguration configuration,IMemoryCache memoryCache,IDistributedCache distributedCache,ICustomMemoryCache customMemoryCache)
        {
            _logger = logger;
            _iConfiguration = configuration;
            _iMemoryCache = memoryCache;
            _iDistributedCache = distributedCache;
            _iCustomMemoryCache = customMemoryCache;
        }
        public IActionResult Index()
        {
           

            base.ViewBag.Port = this._iConfiguration["port"];
            base.ViewBag.Now = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss fff");

            #region MemoryCacha
            {
                string key = $"SecondController-Info";
                if (this._iMemoryCache.TryGetValue<string>(key, out string time))
                {
                    //能得到
                }
                else 
                {
                    time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff");
                    this._iMemoryCache.Set(key,time,TimeSpan.FromSeconds(120));
                }
                base.ViewBag.MemoryCacheNow = time;

                //this._iMemoryCache.Remove(key);
            }
            //缓存到了 MemoryCache   存在进程里面-----重启可以释放-----内存读写快，数据可丢失，体积有限
            #endregion

            #region  解决缓存在不同实例共享问题
            {
                string keyDistributedCache = $"SecondController-Info";
                string time = this._iDistributedCache.GetString(keyDistributedCache);
                if (!string.IsNullOrWhiteSpace(time))
                {
                }
                else
                {
                    time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff");
                    this._iDistributedCache.SetString(keyDistributedCache, time, new DistributedCacheEntryOptions()
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(120)
                    });
                }
                base.ViewBag.DistributedCache = time;

            }

            #endregion

            #region 自定义缓存
            {
                string key = $"SecondController-Info";
                if (this._iCustomMemoryCache.TryGetValue(key, out object time))
                {
                    //能得到
                }
                else
                {
                    time = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff");
                    this._iCustomMemoryCache.Set(key, time);
                }
                base.ViewBag.CustomMemoryCache = time;

            }
            #endregion

            return View();
        }
    }
}
